package hhm.framessm.search.service.impl;

import java.util.List;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hhm.framessm.common.pojo.FBResult;
import hhm.framessm.common.utils.ExceptionUtil;
import hhm.framessm.mapper.TbContentMapper;
import hhm.framessm.pojo.TbContent;
import hhm.framessm.search.service.ContentService;

@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper mapper;

	@Autowired
	private SolrClient solrServer;

	@Override
	public FBResult importAllItems() {
		try {

			// 查询商品列表
			List<TbContent> list = mapper.selectByExample(null);
			// 把商品信息写入索引库
			for (TbContent cont : list) {
				// 创建一个SolrInputDocument对象
				SolrInputDocument document = new SolrInputDocument();
				document.setField("id", cont.getContentId());
				document.setField("item_title", cont.getTitle());
				document.setField("item_content", cont.getContent());
				document.setField("item_keywords", cont.getKeywords());
				// 写入索引库
				solrServer.add(document);
			}
			// 提交修改
			solrServer.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return FBResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		return FBResult.ok();
	}

}
